package com.zjl.reptile.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * ClassName: util
 * Package: com.zjl.reptile
 * Description:
 *
 * @Author 张蛟龙
 * @Create 2024/7/24 19:32
 * @Version 1.0
 */
public class URLUtil {

    public static void main(String[] args) {
        try {
            String urlToRead = "https://www.zhihu.com/"; // 替换为你想访问的网址
            URL url = new URL(urlToRead);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为GET
            connection.setRequestMethod("GET");

            // 接收响应码
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 创建BufferedReader读取响应


                // 打开连接并创建输入流
                BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));

                String line;
                StringBuilder content = new StringBuilder();

                // 逐行读取网页内容并保存到StringBuilder中
                while ((line = reader.readLine()) != null) {
                    content.append(line);
                }

                reader.close();

                // 打印网页内容
                System.out.println(content.toString());
            } else {
                System.out.println("GET request not worked");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void xxx(){
        // 设置浏览器驱动路径
//        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", "D:\\work\\gitee\\reptile\\src\\main\\resources\\chromedriver.exe");

        // 创建ChromeDriver对象
        WebDriver driver = new ChromeDriver();

        try {
            // 打开目标网页
            driver.get("https://baidu.com");

            // 获取网页源代码
            String pageSource = driver.getPageSource();

            // 打印网页源代码
            System.out.println(pageSource);
        } finally {
            // 关闭浏览器驱动
            driver.quit();
        }
    }


    public void xxx2(){
        try {
            // 发送HTTP GET请求并获取网页内容
            Document document = Jsoup.connect("https://www.zhihu.com/search?type=content&q=ai").get();

            System.out.println(document.body().getElementById("a"));
            // 获取网页标题
            String title = document.title();
            System.out.println("网页标题：" + title);

            // 获取所有的链接
            Elements links = document.select("a[href]");
            System.out.println("链接数量：" + links.size());

            // 打印每个链接的文本和URL
            for (Element link : links) {
                String linkText = link.text();
                String linkUrl = link.attr("href");
                System.out.println("链接文本：" + linkText);
                System.out.println("链接URL：" + linkUrl);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
